﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SDT.Lib.Paging;
using System.Linq.Dynamic;
using SDT.Lib.Common;
namespace SDT.Model
{
    public class ArticleManager
    {
        public static ArticleCategory GetCategory(int catId)
        {
            var dataContext = new SDTDataContext();
            return dataContext.ArticleCategories.SingleOrDefault(c => c.ID == catId);
        }
        public static List<int> GetChild(int catid)
        {
            try
            {
                var db = new SDTDataContext();
                var returnVal = new List<int>();
                var curentCat = db.ArticleCategories.Single(o => o.ID == catid);
                returnVal.Add(curentCat.ID);

                foreach (var item in curentCat.ArticleCategories)
                {
                    returnVal.AddRange(GetChild(item.ID));
                }

                return returnVal;
            }
            catch (Exception exp)
            {
                return null;
            }
        }
        public static List<ArticleCategory> GetChildCategory(int catid)
        {
            try
            {
                var db = new SDTDataContext();
                var returnVal = new List<ArticleCategory>();
                var curentCat = db.ArticleCategories.Single(o => o.ID == catid);
                returnVal.Add(curentCat);

                foreach (var item in curentCat.ArticleCategories)
                {
                    returnVal.AddRange(GetChildCategory(item.ID));
                }

                return returnVal;
            }
            catch (Exception exp)
            {
                return null;
            }
        }
        public static List<ArticleCategory> GetListCategory()
        {
            try
            {
                var db = new SDTDataContext();
                var returnVal = new List<ArticleCategory>();
                var listParent = db.ArticleCategories.Where(e => e.ParentID == null).OrderBy(string.Format("{0} {1}", "Priority", "asc")).ToList();
                foreach (var item in listParent)
                {
                    var l = GetChildCategory(item.ID);
                    returnVal.AddRange(l);
                }

                return returnVal;
            }
            catch (Exception exp)
            {
                return null;
            }
        }
        public static Article GetArticle(int articleId)
        {
            var db = new SDTDataContext();
            return db.Articles.SingleOrDefault(a => a.ID == articleId && a.PubYN == true);
        }
        public static IPagedList<Article> GetArticles(int catID, string keyword, int hotYN, int pubYN, string order, string orderDir, int page, int pageSize)
        {
            var dataContext = new SDTDataContext();
            var listcat = catID > 0 ? GetChild(catID) : new List<int>(); 
            var articles = dataContext.Articles
                .Where(a => catID > 0 ? listcat.ToArray().Contains(a.CatID.Value) : true)
                .Where(a => hotYN > 0 ? a.HotYN == (hotYN == 1 ? true : false) : true)
                .Where(a => pubYN > 0 ? a.PubYN == (pubYN == 1 ? true : false) : true)
                .Where(a => keyword==string.Empty ? true : (a.Title.Contains(keyword.ToLower()) || (a.Summary.ToLower().Contains(keyword.ToLower()))))
                .OrderBy(string.Format("{0} {1}", order, orderDir)).AsQueryable();
            var returnVal = articles.ToPagedList(page, pageSize);
            return returnVal;
        }
        public static IList<Article> GetList(int catID, string keyword, int hotYN, int pubYN, string order, string orderDir, int number, int skip)
        {
            var dataContext = new SDTDataContext();
            var listcat =catID>0? GetChild(catID):new List<int>();
            var l = dataContext.Articles
                .Where(a => catID > 0 ? listcat.ToArray().Contains(a.CatID.Value) : true)
                .Where(a => hotYN > 0 ? a.HotYN == true : true)
                .Where(a => pubYN > 0 ? a.PubYN == true : true)
                .OrderBy(string.Format("{0} {1}", order, orderDir)).AsQueryable();
            var rt=l.Skip(skip).Take(number).ToList();
            return rt;
        }
        public static IList<Comment> GetComments(int id, bool? publishYN, int number)
        {
            var dataContext = new SDTDataContext();
            var l = (from c in dataContext.Comments
                     from m in dataContext.CommentsInArticles
                     where c.ID == m.CommentID && m.ArticleID == id && (publishYN == null ? true : c.PublishYN == publishYN)
                     orderby c.ID descending
                     select c).Take(number).ToList();
            return l;
        }
        public static void Delete(int id, string pathRootImg, string rootPathDoc)
        {
            var eDataContext = new SDTDataContext();
            var item = eDataContext.Articles.Single(e => e.ID == id);
            if (item != null)
            {
                if (item.ImagePath != null && item.ImagePath != string.Empty)
                {
                    var filePath = pathRootImg + item.ID + "_" + item.ImagePath;
                    Uploads.DeleteFile(filePath);
                }
                // Delete articleCompanyMap
                var commentInArt = eDataContext.CommentsInArticles.Where(e => e.ArticleID == id).ToList();
                if (commentInArt != null && commentInArt.Count > 0)
                {
                    var arrC = commentInArt.Select(e => e.Comment).ToList();
                    eDataContext.CommentsInArticles.DeleteAllOnSubmit(commentInArt);
                    eDataContext.Comments.DeleteAllOnSubmit(arrC);
                    eDataContext.SubmitChanges();
                }
                // Delete articleCompanyMap
                var artCompany = eDataContext.ArticleCompanyMaps.Where(e => e.ArticleID == id).ToList();
                eDataContext.ArticleCompanyMaps.DeleteAllOnSubmit(artCompany);

                // Delete articleEventMap
                var artEvent = eDataContext.ArticleEventMaps.Where(e => e.ArticleID == id).ToList();
                eDataContext.ArticleEventMaps.DeleteAllOnSubmit(artEvent);

                // Delete articleTagMap
                var artTag = eDataContext.ArticleTagMaps.Where(e => e.ArticleID == id).ToList();
                eDataContext.ArticleTagMaps.DeleteAllOnSubmit(artTag);

                // List Document
                var listDocument = eDataContext.DocumentsInArticles.Where(e => e.ArticleID == id).Select(e => e.Document).ToList();
                // Delete file in folder
                foreach (var currentDocument in listDocument)
                {
                    var filePath = rootPathDoc + currentDocument.ID + "\\" + currentDocument.Path;
                    Uploads.DeleteFile(filePath);
                }

                // Delete Document in article 
                var documentInArt = eDataContext.DocumentsInArticles.Where(e => e.ArticleID == id).ToList();
                eDataContext.DocumentsInArticles.DeleteAllOnSubmit(documentInArt);
                eDataContext.SubmitChanges();

                // Delete list Document
                eDataContext.Documents.DeleteAllOnSubmit(listDocument);
                // Delete current article
                eDataContext.Articles.DeleteOnSubmit(item);
                eDataContext.SubmitChanges();
            }
        }
    }
}
